package cn.admobiletop.adsuyi.adapter.toutiao.loader;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.ad.ADSuyiNativeAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.entity.ADSuyiAdSize;
import cn.admobiletop.adsuyi.ad.entity.ADSuyiExtraParams;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.ADSuyiIniter;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.NativeExpressAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.manager.TTAdInitManager;
import cn.admobiletop.adsuyi.adapter.toutiao.util.TTAdUtil;
import cn.admobiletop.adsuyi.config.ADSuyiInitConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 信息流广告加载器
 * @date 2020/4/3
 */
public class NativeAdLoader implements ADSuyiAdapterLoader<ADSuyiNativeAd, ADSuyiNativeAdListener> {
    private NativeExpressAdListener nativeExpressAdListener;

    @Override
    public void loadAd(ADSuyiNativeAd nativeAd, ADSuyiAdapterParams adapterParams, ADSuyiNativeAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(nativeAd)
                && adapterParams != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId platformPosId = adapterParams.getPlatformPosId();

            ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
            if (config != null && config.isFilterThirdQuestion()) {
                if (TTAdUtil.withoutLowSystem()) {
                    adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "过滤信息流广告，经过测试头条的模板广告在安卓5.1及以下版本的手机上可能存在兼容性问题"));
                    return;
                }
            }

            ADSuyiExtraParams localExtraParams = nativeAd.getLocalExtraParams();
            if (localExtraParams == null || localExtraParams.getAdSize() == null) {
                adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "头条信息流模板广告需要设置预期的size，请通过ADSuyiNativeAd的setLocalExtraParams()方法进行相关设置"));
                return;
            }

            ADSuyiAdSize adSize = localExtraParams.getAdSize();
            if (adSize.getWidth() <= 0) {
                adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "头条信息流模板广告ADSuyiAdSize中的宽度必须大于0"));
                return;
            }

            TTAdNative ttAdNative = TTAdInitManager.getInstance().createNative(nativeAd.getActivity());
            if (ttAdNative == null) {
                adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "头条SDK createNative失败，可能初始化失败或初始化数据有误"));
                return;
            }

            float density = ADSuyiSdk.getInstance().getInitiallyDensity();

            AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(platformPosId.getPlatformPosId())
                    .setSupportDeepLink(true)
                    .setAdCount(adapterParams.getCount())
                    // 设置头条信息流模板广告预期的size，单位为dp，其中height为0的话及高度自适应
                    .setExpressViewAcceptedSize(adSize.getWidth() / density, adSize.getHeight() <= 0 ? 0 : adSize.getHeight() / density)
                    // 这个参数设置即可，不影响个性化模板广告的size
                    .setImageAcceptedSize(640, 320)
                    .setDownloadType(TTAdUtil.getDisplayDownloadTipsDialogType())
                    .build();

            nativeExpressAdListener = new NativeExpressAdListener(adapterParams.getPosId(), adSize.getWidth(), adSize.getHeight(), nativeAd, platformPosId.getPlatformPosId(), adListener);
            ttAdNative.loadNativeExpressAd(adSlot, nativeExpressAdListener);
        }
    }

    @Override
    public void onResumed() {

    }

    @Override
    public void onPaused() {

    }

    @Override
    public void release() {
        if (nativeExpressAdListener != null) {
            nativeExpressAdListener.release();
            nativeExpressAdListener = null;
        }
    }
}
